using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Caching;
using Microsoft.SharePoint;

namespace ipi.MCC.Global
{    
    public class HandleCache
    {
        private const string sPartSeperator = "||";
        private const string scSessionNodeCount = "MCCNodeCount";
        private const string scSessionNodeSubDirCount = "MCCNodeSubDirCount";
        private const string scSessionNodeHasSubDir = "MCCNodeHasSubDir";

        private static string GetCacheString(HandleCacheType hctCurrent)
        {
            string sResult = string.Empty;
            switch (hctCurrent)
            {
                case HandleCacheType.NodeDirCount:
                    sResult = scSessionNodeCount;
                    break;

                case HandleCacheType.NodeSubDirCount:
                    sResult = scSessionNodeSubDirCount;
                    break;

                case HandleCacheType.NodeHasSubDir:
                    sResult = scSessionNodeHasSubDir;
                    break;

                default:
                    break;
            }

            return sResult;
        }

        private static string GetKeyPrefix(SPSite spsCurrent, HandleCacheType hctCurrent)
        {
            return spsCurrent.ID.ToString("N") + sPartSeperator + GetCacheString(hctCurrent);
        }

        private static string GetKey(SPSite spsCurrent, HandleCacheType hctCurrent, System.Web.UI.Page pgeCurrent, string sNodeID)
        {
            string sPrefix = GetKeyPrefix(spsCurrent, hctCurrent);
            return sPrefix + sPartSeperator + pgeCurrent.User.Identity.Name + sPartSeperator + sNodeID;
        }
        
        public static void ClearSiteCache(System.Web.UI.Page pgeCurrent, SPSite spsCurrent)
        {
            string sPrefixNodeDirCount = GetKeyPrefix(spsCurrent, HandleCacheType.NodeDirCount);
            string sPrefixNodeSubDirCount = GetKeyPrefix(spsCurrent, HandleCacheType.NodeSubDirCount);
            string sPrefixNodeHasSubDir = GetKeyPrefix(spsCurrent, HandleCacheType.NodeHasSubDir);
            
            System.Collections.Specialized.StringCollection scKeys = new System.Collections.Specialized.StringCollection();

            System.Collections.IDictionaryEnumerator de = pgeCurrent.Cache.GetEnumerator();
            while (de.MoveNext())
            {
                string sKey = (string)de.Key;
                if (sKey.Contains(sPrefixNodeDirCount) ||
                    sKey.Contains(sPrefixNodeSubDirCount) ||
                    sKey.Contains(sPrefixNodeHasSubDir))
                {
                    scKeys.Add(sKey);
                }
            }

            foreach (string sCurrentKey in scKeys)
            {
                pgeCurrent.Cache.Remove(sCurrentKey);
            }
        }
       
        public static void SetValue(System.Web.UI.Page pgeCurrent, SPSite spsCurrent, HandleCacheType hctCurrent, string sNodeID, object sValue)
        {
            string sKey = GetKey(spsCurrent, hctCurrent, pgeCurrent, sNodeID);

            if (pgeCurrent.Cache[sKey] != null)
			{
                pgeCurrent.Cache.Remove(sKey);	
			}

            pgeCurrent.Cache.Insert(sKey, sValue, null, System.DateTime.MaxValue, TimeSpan.FromDays(1), CacheItemPriority.NotRemovable, null);
        }

        public static bool ExistsSettingsForKey(System.Web.UI.Page pgeCurrent, SPSite spsCurrent, int iNodeID)
        {
            string sKeyNodeDirCount = GetKey(spsCurrent, HandleCacheType.NodeDirCount, pgeCurrent, iNodeID.ToString());
            string sKeyNodeSubDirCount = GetKey(spsCurrent, HandleCacheType.NodeSubDirCount, pgeCurrent, iNodeID.ToString());
            string sKeyNodeHasSubDir = GetKey(spsCurrent, HandleCacheType.NodeHasSubDir, pgeCurrent, iNodeID.ToString());

            return ((pgeCurrent.Cache[sKeyNodeDirCount] != null) &&
                    (pgeCurrent.Cache[sKeyNodeSubDirCount] != null) &&
                    (pgeCurrent.Cache[sKeyNodeHasSubDir] != null));
        }

        #region Override
        public static void ClearSiteCache(System.Web.UI.Page pgeCurrent, SPWeb spwCurrent)
        {
            ClearSiteCache(pgeCurrent, spwCurrent.Site);
        }

        public static void SetValue(System.Web.UI.Page pgeCurrent, SPWeb spwCurrent, HandleCacheType hctCurrent, int iNodeID, object sValue)
        {
            SetValue(pgeCurrent, spwCurrent.Site, hctCurrent, iNodeID.ToString(), sValue);
        }

        public static void SetValue(System.Web.UI.Page pgeCurrent, SPWeb spwCurrent, HandleCacheType hctCurrent, string sNodeID, object sValue)
        {
            SetValue(pgeCurrent, spwCurrent.Site, hctCurrent, sNodeID, sValue);
        }

        public static void SetValue(System.Web.UI.Page pgeCurrent, SPSite spsCurrent, HandleCacheType hctCurrent, int iNodeID, object sValue)
        {
            SetValue(pgeCurrent, spsCurrent, hctCurrent, iNodeID.ToString(), sValue);
        }

        public static object GetValue(System.Web.UI.Page pgeCurrent, SPWeb spwCurrent, HandleCacheType hctCurrent, int iNodeID)
        {
            return GetValue(pgeCurrent, spwCurrent.Site, hctCurrent, iNodeID.ToString());
        }

        public static object GetValue(System.Web.UI.Page pgeCurrent, SPWeb spwCurrent, HandleCacheType hctCurrent, string sNodeID)
        {
            return GetValue(pgeCurrent, spwCurrent.Site, hctCurrent, sNodeID);
        }

        public static object GetValue(System.Web.UI.Page pgeCurrent, SPSite spsCurrent, HandleCacheType hctCurrent, int iNodeID)
        {
            return GetValue(pgeCurrent, spsCurrent, hctCurrent, iNodeID.ToString());
        }

        public static object GetValue(System.Web.UI.Page pgeCurrent, SPSite spsCurrent, HandleCacheType hctCurrent, string sNodeID)
        {
            return pgeCurrent.Cache[GetKey(spsCurrent, hctCurrent, pgeCurrent, sNodeID)];
        } 
        #endregion

    }
}
